﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DHQGHN.PM2.Models
{
    public class EnumeratorCustomModelBinder : IModelBinder
    {
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            HttpRequestBase request = controllerContext.HttpContext.Request;

            string ENUMERATOR_ID = request.Form.Get("ENUMERATOR_ID");
            string ADM_UNIT_ID = request.Form.Get("ADM_UNIT_ID");
            string ENUM_CODE = request.Form.Get("ENUM_CODE");
            string FULL_NAME = request.Form.Get("FULL_NAME");
            string SIGNATURE = request.Form.Get("SIGNATURE");            
            //string DESCRIPTIONS = request.Form.Get("DESCRIPTIONS");
            string DESCRIPTIONS = request.Unvalidated.Form["DESCRIPTIONS"];
            string USERNAME = request.Form.Get("USERNAME");
            string GROUPCODE = request.Form.Get("GROUPCODE");
            string GROUP_CODE_OLD = request.Form.Get("GROUP_CODE_OLD");
            string ADM_CODE = request.Form.Get("ADM_CODE");
            string ADM_NAME = request.Form.Get("ADM_NAME");
            string AREA_NAME = request.Form.Get("AREA_NAME");
            string PROVINCE_NAME = request.Form.Get("PROVINCE_NAME");
            string DISTRICT_NAME = request.Form.Get("DISTRICT_NAME");
            string COMMUNE_NAME = request.Form.Get("COMMUNE_NAME ");
            string EMAIL = request.Form.Get("EMAIL");

            return new EnumeratorModel
            {
                ENUMERATOR_ID = ENUMERATOR_ID,
                ADM_UNIT_ID = ADM_UNIT_ID,
                ENUM_CODE = ENUM_CODE,
                FULL_NAME = FULL_NAME != null ? FULL_NAME.Trim() : FULL_NAME,
                SIGNATURE = SIGNATURE,
                DESCRIPTIONS = DESCRIPTIONS != null ? DESCRIPTIONS.Trim() : DESCRIPTIONS,
                USERNAME = USERNAME,
                GROUPCODE = GROUPCODE,
                GROUP_CODE_OLD = GROUP_CODE_OLD,
                ADM_CODE = ADM_CODE,
                ADM_NAME = ADM_NAME != null ? ADM_NAME.Trim() : ADM_NAME,
                AREA_NAME = AREA_NAME != null ? AREA_NAME.Trim() : AREA_NAME,
                PROVINCE_NAME = PROVINCE_NAME,
                DISTRICT_NAME = DISTRICT_NAME,
                COMMUNE_NAME = COMMUNE_NAME,
                EMAIL = EMAIL != null ? EMAIL.Trim() : EMAIL
            };
        }

        public static ValueProviderResult GetValueFromValueProvider(ModelBindingContext bindingContext, bool performRequestValidation)
        {
            var unvalidatedValueProvider = bindingContext.ValueProvider as IUnvalidatedValueProvider;

            return (unvalidatedValueProvider != null)

                       ? unvalidatedValueProvider.GetValue(bindingContext.ModelName, !performRequestValidation)

                       : bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

        }

    }
}